/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.LoggerFactory;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author GiangLT
 */
public class MailUtil {

    public static enum MailParams {

        SMTP_HOST,
        SMTP_PORT,
        MAIL_SENDERS,
        TO_RECEIVERS,
        CC_RECEIVERS,
        BCC_RECEIVERS,
        MAIL_SUBJECT,
        MAIL_CONTENT,
        MAIL_ATTACHMENTS
    }
    private static MailUtil instance;

    public static MailUtil getInstance() {
        return instance = (instance != null) ? instance : new MailUtil();
    }

    public static void sendMail(Map<MailParams, Object> params) throws
            AddressException, MessagingException {
        // Create a mail session
        java.util.Properties props = new java.util.Properties();
        props.put("mail.smtp.host", params.get(MailParams.SMTP_HOST).toString());
        props.put("mail.smtp.port", "" + Integer.parseInt(params.get(MailParams.SMTP_PORT).toString()));
        Session session = Session.getDefaultInstance(props, null);

        // Construct the message
        // Add subject
        Message msg = new MimeMessage(session);
        msg.setSubject(params.get(MailParams.MAIL_SUBJECT).toString());

        // Add from
        String[] asFrom = (String[]) params.get(MailParams.MAIL_SENDERS);
        InternetAddress[] from = new InternetAddress[asFrom.length];
        for (int i = 0; i < asFrom.length; i++) {
            from[i] = new InternetAddress(asFrom[i]);
        }
        msg.addFrom(from);

        // Add to
        String[] asTo = (String[]) params.get(MailParams.TO_RECEIVERS);
        InternetAddress[] to = new InternetAddress[asTo.length];
        for (int i = 0; i < asTo.length; i++) {
            to[i] = new InternetAddress(asTo[i]);
        }
        msg.addRecipients(Message.RecipientType.TO, to);

        // Add cc
        String[] asCc = (String[]) params.get(MailParams.CC_RECEIVERS);
        if (asCc != null) {
            InternetAddress[] cc = new InternetAddress[asCc.length];
            for (int i = 0; i < asCc.length; i++) {
                cc[i] = new InternetAddress(asCc[i]);
            }
            msg.addRecipients(Message.RecipientType.CC, cc);
        }

        // Add bcc
        String[] asBcc = (String[]) params.get(MailParams.BCC_RECEIVERS);
        if (asBcc != null) {
            InternetAddress[] bcc = new InternetAddress[asBcc.length];
            for (int i = 0; i < asBcc.length; i++) {
                bcc[i] = new InternetAddress(asBcc[i]);
            }
            msg.addRecipients(Message.RecipientType.BCC, bcc);
        }


        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setText(params.get(MailParams.MAIL_CONTENT).toString());

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(bodyPart);

        // Add attachments
        DataSource dataSource = null;
        String[] fileNames = (String[]) params.get(MailParams.MAIL_ATTACHMENTS);
        for (String fileName : fileNames) {
            File file = new File(fileName);
            dataSource = new FileDataSource(file);
            bodyPart = new MimeBodyPart();
            bodyPart.setDataHandler(new DataHandler(dataSource));
            bodyPart.setFileName(fileName);
            multipart.addBodyPart(bodyPart);
        }

        msg.setContent(multipart);
        // Send the message
        Transport.send(msg);
    }

    @Deprecated
    public static void sendMail(String smtpHost, int smtpPort,
            String asFrom[], String[] asTo,
            String subject, String content) throws
            AddressException, MessagingException {
        // Create a mail session
        java.util.Properties props = new java.util.Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", "" + smtpPort);
        Session session = Session.getDefaultInstance(props, null);

        // Construct the message
        Message msg = new MimeMessage(session);
        InternetAddress[] from = new InternetAddress[asFrom.length];
        for (int i = 0; i < asFrom.length; i++) {
            from[i] = new InternetAddress(asFrom[i]);
        }
        msg.addFrom(from);

        InternetAddress[] to = new InternetAddress[asTo.length];
        for (int i = 0; i < asTo.length; i++) {
            to[i] = new InternetAddress(asTo[i]);
        }
        msg.addRecipients(Message.RecipientType.TO, to);

        msg.setSubject(subject);
        msg.setText(content);

        // Send the message
        Transport.send(msg);
    }

    public static void main(String[] args) {
        String smtpHost = "mail.elcom.com.vn";
        int smtpPort = 25;
        String asFrom[] = {
            "gianglt@elcom.com.vn"
        };
        String asTo[] = {
            "gianglt@elcom.com.vn"
        };
        String subject = "Test Subject";
        String content = "Test Content";

        String[] file = {
            "D:\\Departments.txt",
            "D:\\Employees.txt"
        };

        Map<MailParams, Object> params = new HashMap<MailParams, Object>();
        params.put(MailParams.SMTP_HOST, smtpHost);
        params.put(MailParams.SMTP_PORT, smtpPort);
        params.put(MailParams.MAIL_SUBJECT, subject);
        params.put(MailParams.MAIL_CONTENT, content);
        params.put(MailParams.MAIL_SENDERS, asFrom);
        params.put(MailParams.TO_RECEIVERS, asTo);
        params.put(MailParams.MAIL_ATTACHMENTS, file);

        try {
            MailUtil.sendMail(params);
        } catch (AddressException ex) {
            LoggerFactory.getLogger(MailUtil.class.getName()).error("", ex);
        } catch (MessagingException ex) {
            LoggerFactory.getLogger(MailUtil.class.getName()).error("", ex);
        }
    }
}
